#include "groupsessiondetailwidget.h"

#include <QVBoxLayout>
#include <QScrollArea>
#include <QScrollBar>
#include <QPushButton>
#include <QHBoxLayout>
#include <QLineEdit>

#include "sessiondetailwidget.h"
#include "debug.h"
#include "model/datacenter.h"

using namespace model;

GroupSessionDetailWidget::GroupSessionDetailWidget(QWidget* parent)
    : QDialog(parent)
{
    // 1. 设置窗口的基本属性
    this->setFixedSize(280, 550);
    this->setWindowTitle("群组详情");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setStyleSheet("QDialog { background-color: rgb(255, 255, 255); }");
    this->setAttribute(Qt::WA_DeleteOnClose);

    // 2. 创建布局管理器
    QVBoxLayout* vlayout = new QVBoxLayout();
    vlayout->setSpacing(0);
    vlayout->setContentsMargins(15, 20, 0, 10);
    vlayout->setAlignment(Qt::AlignTop);
    this->setLayout(vlayout);

    // 2.5
    // 搜索框
    QHBoxLayout* searchLayout = new QHBoxLayout();
    searchLayout->setContentsMargins(5, 0, 0, 0);
    vlayout->addLayout(searchLayout);

    QWidget* searchWidget = new QWidget();
    searchWidget->setFixedHeight(60);
    searchWidget->setLayout(searchLayout);

    QAction* search = new QAction();
    search->setIcon(QIcon(":/resource/image/search.png"));
    QLineEdit* searchEdit = new QLineEdit();
    searchEdit->setFixedSize(230, 26);
    searchEdit->setPlaceholderText("搜索群成员");
    searchEdit->setStyleSheet("QLineEdit { border-radius: 4px; background-color: rgb(232, 232, 232); }"
                              "QLineEdit:focus { background-color: rgb(250, 250, 250);"
                              "border: 1px solid rgb(241, 241, 241); }");
    searchEdit->addAction(search, QLineEdit::LeadingPosition);

    QWidget* spacer1 = new QWidget();
    spacer1->setFixedHeight(25);

    searchLayout->addWidget(searchEdit);
    vlayout->addWidget(spacer1);
    //searchLayout->addWidget(spacer1);

    // 3. 创建滚动区域
    // 3.1 创建 QScrollArea 对象
    QScrollArea* scrollArea = new QScrollArea();
    scrollArea->setWidgetResizable(true);
    scrollArea->verticalScrollBar()->setStyleSheet("QScrollBar:vertical { width: 2px; "
                                                   "background-color: rgb(255, 255, 255);}");
    scrollArea->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0; }");
    scrollArea->setFixedSize(250, 310);
    scrollArea->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
    scrollArea->setStyleSheet("QWidget { background-color: transparent; border: none; }");

    // 3.2 创建一个 QScrollArea 内部的 QWidget
    QWidget* container = new QWidget();
    scrollArea->setWidget(container);

    // 3.3 给 container 里面添加一个 网格布局
    glayout = new QGridLayout();
    glayout->setVerticalSpacing(18);
    glayout->setHorizontalSpacing(0);
    glayout->setContentsMargins(3, 0, 0, 0);
    glayout->setAlignment(Qt::AlignTop | Qt::AlignLeft);
    container->setLayout(glayout);

    // 3.4 把滚动区域 添加到布局管理器中
    vlayout->addWidget(scrollArea);

    // 3.5 横线
    QWidget* spacer2 = new QWidget();
    spacer2->setFixedHeight(24);
    vlayout->addWidget(spacer2);

    QFrame *line = new QFrame();
    line->setFrameShape(QFrame::HLine);
    line->setStyleSheet("QFrame { color: rgb(236, 236, 236); }");
    vlayout->addWidget(line);

    QWidget* spacer3 = new QWidget();
    spacer3->setFixedHeight(24);
    vlayout->addWidget(spacer3);

    // 4. 添加 "添加" 按钮
    AvatarItem* addBtn = new AvatarItem(QIcon(":/resource/image/cross.png"), "添加");
    glayout->addWidget(addBtn, 0, 0);

    // 5. 添加 "群聊名称"
    QLabel* groupNameTag = new QLabel();
    groupNameTag->setText("群聊名称");
    groupNameTag->setStyleSheet("QLabel { font-weight: 450; font-size: 14px; }");
    groupNameTag->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    groupNameTag->setFixedHeight(20);
    vlayout->addWidget(groupNameTag, 0, Qt::AlignBottom);

    // 6. 添加 真实的群名称 和 修改按钮
    // 6.1 创建水平布局
    QHBoxLayout* hlayout = new QHBoxLayout();
    hlayout->setSpacing(0);
    hlayout->setContentsMargins(0, 0, 0, 0);
    vlayout->addLayout(hlayout);

    // 6.2 创建真实的群聊名字的 label
    groupNameLabel = new QLabel();
    groupNameLabel->setStyleSheet("QLabel { font-weight: 450; font-size: 14px; color: rgb(127, 127, 127); }");
    groupNameLabel->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Expanding);
    groupNameLabel->setFixedHeight(20);
    hlayout->addWidget(groupNameLabel, 0, Qt::AlignLeft | Qt::AlignVCenter);

    // 6.3 创建 "修改按钮"
    QPushButton* modifyBtn = new QPushButton();
    modifyBtn->setFixedSize(30, 30);
    modifyBtn->setIconSize(QSize(20, 20));
    modifyBtn->setIcon(QIcon(":/resource/image/modify.png"));
    modifyBtn->setStyleSheet("QPushButton { border: none; background-color: transparent; }"
                             "QPushButton:pressed { icon: url(':/resource/image/modify_clicked.png'); }");
    hlayout->addWidget(modifyBtn, 0, Qt::AlignRight | Qt::AlignVCenter);

    // 7. 退出群聊按钮
    QWidget* spacer4 = new QWidget();
    spacer4->setFixedHeight(15);
    vlayout->addWidget(spacer4);


    QPushButton* exitGroupBtn = new QPushButton();
    exitGroupBtn->setFixedSize(250, 30);
    exitGroupBtn->setText("退出群聊");
    QString style = "QPushButton { border: 1px solid rgb(205, 205, 205); border-radius: 5px; "
                    "color: red; }";
    style += "QPushButton::pressed { color: rgb(251, 165, 151);}";
    exitGroupBtn->setStyleSheet(style);
    vlayout->addWidget(exitGroupBtn);

#if TEST_UI
    groupNameLabel->setText("春日部防卫队");
    QIcon avatar(":/resource/image/Mitsy.jpg");
    for (int i = 0; i < 20; ++i)
    {
        AvatarItem* item = new AvatarItem(avatar, "野原美伢" + QString::number(i));
        this->addMember(item);
    }
#endif


    // 8. 从服务器加载数据
    initData();
}

void GroupSessionDetailWidget::addMember(AvatarItem *avatarItem)
{
    // 拿到滚动区中的布局管理器，取到成员变量即可
    const int MAX_COL = 4;
    if (curCol >= MAX_COL)
    {
        ++curRow;
        curCol = 0;
    }
    glayout->addWidget(avatarItem, curRow, curCol);
    ++curCol;
}

void GroupSessionDetailWidget::initData()
{
    DataCenter* dataCenter = DataCenter::getInstance();
    connect(dataCenter, &DataCenter::getMemberListDone, this, &GroupSessionDetailWidget::initMembers);
    dataCenter->getMemberListAsync(dataCenter->getCurrentChatSessionId());
}

void GroupSessionDetailWidget::initMembers(const QString& chatSessionId)
{
    DataCenter* dataCenter = DataCenter::getInstance();
    QList<UserInfo>* memberList = dataCenter->getMemberList(chatSessionId);
    if (memberList == nullptr)
    {
        LOG("获取的成员列表为空! chatSessionId = ") << chatSessionId;
        return ;
    }

    // 遍历成员
    for (const auto& u : *memberList)
    {
        AvatarItem* avatarItem = new AvatarItem(u.avatar, u.nickname);
        this->addMember(avatarItem);
    }

    // 暂时固定群聊名称
    groupNameLabel->setText("新的群聊");
}
















